<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>公告管理</title>
<link rel="stylesheet" href="/resources/layui/css/layui.css" media="all">
</head>
<body>
<!-- 搜索开始 -->
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  		<legend>查询条件</legend>
	</fieldset>
	<form class="layui-form" action="" id="searchFrm">
		<div class="layui-form-item" style="text-align: center">
		    <div class="layui-inline">
		      <label class="layui-form-label">公告标题</label>
		      <div class="layui-input-inline">
		        <input type="text" name="title"  autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-inline">
		      <label class="layui-form-label">开始时间</label>
		      <div class="layui-input-inline">
		        <input type="text" name="startTime"   autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-inline">
		      <label class="layui-form-label">结束时间</label>
		      <div class="layui-input-inline">
		        <input type="text" name="endTime"  autocomplete="off" class="layui-input">
		      </div>
		    </div>
	  </div>
	  <div class="layui-form-item" style="text-align: center;">
		    <div class="layui-input-block">
		      <button type="button" class="layui-btn" id="doSearch">查询</button>
		      <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
		    </div>
  	 </div>
	</form>
	<!-- 搜索结束-->
	<table class="layui-hide" id="noticeList" lay-filter="noticeList"></table>
	<div id="tableToolBar" style="display: none;">
		<button type="button" class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="notice:create">添加</button>
		<button type="button" class="layui-btn layui-btn-sm  layui-btn-danger" lay-event="batchDelete" shiro:hasPermission="notice:batchdelete">批量删除</button>
	</div>
	<div id="tableCellToolBar" style="display: none;">
		<button type="button" class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="edit" shiro:hasPermission="notice:update">修改</button>
		<button type="button" class="layui-btn layui-btn-sm  layui-btn-danger" lay-event="del" shiro:hasPermission="notice:delete">删除</button>
		<button type="button" class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="show" shiro:hasPermission="notice:viewnotice">查看</button>
	</div>
</body>
<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
var tableIns;
layui.use([ 'layer', 'jquery', 'table', 'form' ], function() {
	var layer = layui.layer;
	var $ = layui.jquery;
	var table = layui.table;
	var from = layui.form;
	//渲染表格
	tableIns = table.render({
		elem : '#noticeList' //渲染表格的容器
		,
		url : '/notice/loadAllNotice' //请求数据的地址
		,
		cellMinWidth : 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
		,
		page : true //开启分页
		,
		toolbar : '#tableToolBar'//设置表头左上角的工具条  并开启表头右上角的工具栏
		,
		defaultToolbar : [ 'filter', 'print' ] //设置工具栏的位置和是否显示
		,
		height:'430px',
		cols : [ [ {
			type : 'checkbox',
			width : 100
		}, {
			field : 'id',
			width : 195,
			title : '公告ID',
			align : 'center'
		}, {
			field : 'title',
			width : 250,
			title : '公告标题',
			align : 'center'
		}, {
			field : 'createtime',
			width : 250,
			title : '发布时间',
			align : 'center'
		}, {
			field : 'opername',
			width : 250,
			title : '发布人',
			align : 'center'
		}, {
			width : 250,
			title : '操作',
			align : 'center',
			toolbar : "#tableCellToolBar",
			fixed : 'right',
			align : 'center'
		} ] ]
	});

	//刷新表格
	$("#doSearch").click(function() {
		var params=$("#searchFrm").serialize();
		tableIns.reload({
			url : '/notice/loadAllNotice?'+params,
			page : {
				curr : 1
			}
		});
	});
	
	
	//监听表头工具栏按钮
	table.on('toolbar(noticeList)', function(obj){
	  switch(obj.event){
	    case 'add':
	      addNotice();
	    break;
	    case 'batchDelete':
	    	var checkStatus = table.checkStatus('noticeList');
	    	var data = checkStatus.data;
	    	if(data.length>0){
	    		batchDelete(data);
	    	}else{
	    		layer.msg("请选择删除项");
	    	}
	    break;
	  };
	});
	
	function addNotice(){
		var index = layer.open({
			title:"添加公告",
			area:["800px","530px"],
			type:2,
			content:"/main/toAddNotice",
			success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回公告列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
		});
	};
	
	//批量删除
	function batchDelete(data){
		layer.confirm('确定要删除选中的所有公告吗?', function(index){
			var ids = "";
			for(var i=0;i<data.length;i++){
				if(i==0){
					ids += "ids="+data[i].id; 
				}else{
					ids += "&ids="+data[i].id;
				}
			}
			$.post("/notice/batchDelete?"+ids,function(obj){
				layer.msg(obj.msg);
	            tableIns.reload();
	            layer.close(index);
			});
	    })
	}
	
	
	//监听表格行工具栏
	table.on('tool(noticeList)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		  var data = obj.data; //获得当前行数据
		  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		 
		   if(layEvent === 'del'){ //删除
		    layer.confirm('确定要删除['+data.title+']这条公告吗?', function(index){
		     	$.post("/notice/deleteNotice",{id:data.id},function(obj){
		     		layer.msg(obj.msg);
		            tableIns.reload();
		            layer.close(index);
		     	});
		    });
		  } else if(layEvent === 'edit'){ //编辑
		    updateNotice(data.id);
		  }else if(layEvent === 'show'){//查看
			  showNotice(data.id);
		  }
		});
	
	function updateNotice(id){
		layer.open({
			title:'修改公告',
			area:["800px","530px"],
			type:2,
			content:'/main/toUpdateNotice?id='+id,
			success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回公告列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
		});
	}
	
	function showNotice(id){
		layer.open({
			title:'查看公告',
			type:2,
			area:["800px","530px"],
			content:'/main/toShowNotice?id='+id,
			success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回公告列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
		});
	}
	

});
</script>
</html>